package com.lsq.baseadmin.modules.maintenance.controller;


import com.lsq.baseadmin.common.enums.SwitchEnum;
import com.lsq.baseadmin.common.exception.MyException;
import com.lsq.baseadmin.modules.maintenance.entity.ScheduleJob;
import com.lsq.baseadmin.modules.maintenance.service.ScheduleJobService;
import com.lsq.baseadmin.modules.maintenance.utils.ScheduleUtils;
import io.swagger.annotations.ApiOperation;
import org.quartz.CronExpression;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 计划任务 前端控制器
 * </p>
 *
 * @author lsq
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/scheduleJob")
public class ScheduleJobController {

    private final ScheduleJobService scheduleJobService;

    public ScheduleJobController(ScheduleJobService scheduleJobService) {
        this.scheduleJobService = scheduleJobService;
    }

    @ApiOperation("查询定时任务")
    @GetMapping(value = "/list")
    public ResponseEntity<Object> list(ScheduleJob job, int pageNum, int pageSize) {
        return ResponseEntity.ok(scheduleJobService.pg(job, pageNum, pageSize));
    }

    @ApiOperation("新增定时任务")
    @PostMapping(value = "/add")
    public ResponseEntity<String> add(@Validated @RequestBody ScheduleJob job) {
        if (!CronExpression.isValidExpression(job.getCronExpression())) {
            throw new MyException(HttpStatus.BAD_REQUEST, "cron表达式格式错误");
        }
        scheduleJobService.save(job);
        ScheduleUtils.createScheduleJob(job);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("修改定时任务")
    @PostMapping(value = "/update")
    public ResponseEntity<Object> update(@Validated @RequestBody ScheduleJob job) {
        if (!CronExpression.isValidExpression(job.getCronExpression())) {
            throw new MyException(HttpStatus.BAD_REQUEST, "cron表达式格式错误");
        }
        scheduleJobService.updateById(job);
        ScheduleUtils.updateScheduleJob(job);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("更改定时任务状态")
    @PostMapping(value = "/updateStatus")
    public ResponseEntity<Object> updateStatus(@RequestParam int id) {
        ScheduleJob updateObj = new ScheduleJob();
        ScheduleJob job = scheduleJobService.getById(id);
        if (SwitchEnum.ENABLE.getValue().equals(job.getStatus())) {
            updateObj.setStatus(SwitchEnum.DISABLE.getValue());
        } else {
            updateObj.setStatus(SwitchEnum.ENABLE.getValue());
        }
        updateObj.setId(id);
        scheduleJobService.updateById(updateObj);

        // 修改定时器运行状态
        if (SwitchEnum.ENABLE.getValue().equals(job.getStatus())) {
            ScheduleUtils.pauseJob(job.getId());
        } else {
            ScheduleUtils.resumeJob(job.getId());
        }
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @ApiOperation("执行定时任务")
    @PostMapping(value = "/exec")
    public ResponseEntity<Object> execution(@RequestParam int id) {
        ScheduleUtils.run(scheduleJobService.getById(id));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("删除定时任务")
    @PostMapping(value = "/delete")
    public ResponseEntity<Object> delete(@RequestParam int id) {
        scheduleJobService.removeById(id);
        ScheduleUtils.deleteScheduleJob(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
